﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
using DevExpress.XtraPrinting.Drawing;
using LayerEntity.Producto;

namespace System2012.Listados
{
    public partial class frmListadoFamilias : DevExpress.XtraEditors.XtraForm
    {
        /// <summary>
        /// 
        /// </summary>
        DataTable dtbFamilia;
        public ArrayList data = new ArrayList();
        /// <summary>
        /// index
        /// </summary>
        int indexRow;
        public frmListadoFamilias()
        {
            this.InitializeComponent();
            //this.txtFiltroFamilia.w
        }

        private void Seleccionar(int index)
        {
            string idfamilia = this.dtbFamilia.Rows[index]["idfamilia"].ToString();
            string familia = this.dtbFamilia.Rows[index]["familia"].ToString();
            string idgrupo = this.dtbFamilia.Rows[index]["idgrupo"].ToString();
            string grupo = this.dtbFamilia.Rows[index]["grupo"].ToString();
            this.data.Add(idfamilia);
            this.data.Add(familia);
            this.data.Add(idgrupo);
            this.data.Add(grupo);
            this.Close();
            // this.Dispose();
        }

        private void BGWFamilias_DoWork(object sender, DoWorkEventArgs e)
        {
            this.BGWFamilias.ReportProgress(30, "Cargando Familias");
            EFamilia obj = new EFamilia();
            obj.Nombre = string.Format("where familia ilike '%{0}%'", this.txtFiltroFamilia.Text);
            e.Result = SystemUtils.RecodManajerproducto.GetListaFamilia(obj);
            this.BGWFamilias.ReportProgress(100, "Completado");
        }

        private void BGWFamilias_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.dtbFamilia = new DataTable();
            this.dtbFamilia = (DataTable)e.Result;
            this.tblGrid.DataSource = this.dtbFamilia;// System2012.SystemUtils.MantenanceHabitacion.getHabitaciones();
            this.tblGrid.RefreshDataSource();
        }

        private void frmListadoFamilias_Load(object sender, EventArgs e)
        {
            Watermark textWatermark = new Watermark();
            if (!this.BGWFamilias.IsBusy)
            {
                this.BGWFamilias.RunWorkerAsync();
            }
            this.txtFiltroFamilia.Properties.NullValuePrompt = "INGRESE EL NOMBRE DE LA FAMILIA";
            this.txtFiltroFamilia.Properties.NullValuePromptShowForEmptyValue = true;
            //  this.txtFiltroFamilia.Properties.NullText = "fdgfgfgf";
        }

        private void txtFiltroFamilia_TextChanged(object sender, EventArgs e)
        {
            if (!this.BGWFamilias.IsBusy)
            {
                this.BGWFamilias.RunWorkerAsync();
            }
        }

        private void txtFiltroFamilia_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                if (this.gridView1.RowCount > 0)
                {
                    if (this.dtbFamilia.Rows.Count > 0)
                    {
                        this.tblGrid.Focus();
                    }
                }
            }
        }

        private void tblGrid_DoubleClick(object sender, EventArgs e)
        {
            this.Seleccionar(this.indexRow);
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            this.indexRow = e.FocusedRowHandle;
        }

        private void txtFiltroFamilia_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down)
            {
                if (this.dtbFamilia.Rows.Count > 0)
                {
                    this.tblGrid.Focus();
                }
            }
        }

        private void gridView1_KeyPress(object sender, KeyPressEventArgs e)
        {
        }

        private void tblGrid_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                this.Seleccionar(this.indexRow);
            }
        }
    }
}